package com.ruoyi.subject.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.subject.domain.QbExamRecord;
import com.ruoyi.subject.service.IQbExamRecordService;
import com.ruoyi.subject.util.QbSubjectUtil;
import com.ruoyi.user.service.IQbUserWxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 考试记录Controller
 *
 * @author lzhj
 * @date 2021-01-27
 */
@Api(tags = "考试记录")
@RestController
@RequestMapping("/qb/sub/record")
public class QbExamRecordController extends BaseController {
    @Autowired
    private IQbExamRecordService qbExamRecordService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IQbUserWxService qbUserWxService;

    /**
     * 查询考试记录列表
     */
    @ApiOperation(value = "查询考试记录列表,userId必传")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", dataType = "int", paramType = "query"),
    })
    @PostMapping("/list")
    public AjaxResult list(QbExamRecord qbExamRecord) {
        if (!qbUserWxService.validataUserInfo(qbExamRecord.getUserId(), qbExamRecord.getOpenId())) {
            return QbSubjectUtil.getWxValidataFailMag();
        }
        if (StringUtils.isNull(qbExamRecord.getUserId())) {
            return AjaxResult.success(new TableDataInfo());
        }
        startPage();
        List<QbExamRecord> list = qbExamRecordService.selectQbExamRecordList(qbExamRecord);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 查询成绩排名
     */
    @ApiOperation(value = "查询成绩排名", notes = "score:分数，user_name：姓名，exam_time：考试时间，end_time：结束时间")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "userId", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "openId", value = "openid", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "subType", value = "题库类型", dataType = "string", paramType = "query"),
    })
    @PostMapping(value = "getRankBySubType")
    public AjaxResult getRankBySubType(String subType, long userId, String openId) {
        if (StringUtils.isBlank(subType)) {
            return AjaxResult.error("参数错误");
        }
        if (!qbUserWxService.validataUserInfo(userId, openId)) {
            return QbSubjectUtil.getWxValidataFailMag();
        }
        List<Map<String, Object>> records = redisCache.getCacheObject(QbSubjectUtil.getExamRecordCacheKey(subType));
        return AjaxResult.success(records);
    }

    /**
     * 导出考试记录列表
     */
    @ApiOperation(value = "导出考试记录列表", hidden = true)
    @PreAuthorize("@ss.hasPermi('qb:record:export')")
    @Log(title = "考试记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QbExamRecord qbExamRecord) {
        List<QbExamRecord> list = qbExamRecordService.selectQbExamRecordList(qbExamRecord);
        ExcelUtil<QbExamRecord> util = new ExcelUtil<QbExamRecord>(QbExamRecord.class);
        return util.exportExcel(list, "record");
    }

    /**
     * 获取考试记录详细信息
     */
    @ApiOperation(value = "获取考试记录详细信息", hidden = true)
    @PreAuthorize("@ss.hasPermi('qb:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(qbExamRecordService.selectQbExamRecordById(id));
    }

    /**
     * 新增考试记录
     */
    @ApiOperation(value = "新增考试记录", hidden = true)
    @PreAuthorize("@ss.hasPermi('qb:record:add')")
    @Log(title = "考试记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QbExamRecord qbExamRecord) {
        return toAjax(qbExamRecordService.insertQbExamRecord(qbExamRecord));
    }

    /**
     * 修改考试记录
     */
    @ApiOperation(value = "修改考试记录", hidden = true)
    @PreAuthorize("@ss.hasPermi('qb:record:edit')")
    @Log(title = "考试记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QbExamRecord qbExamRecord) {
        return toAjax(qbExamRecordService.updateQbExamRecord(qbExamRecord));
    }

    /**
     * 删除考试记录
     */
    @ApiOperation(value = "删除考试记录", hidden = true)
    @PreAuthorize("@ss.hasPermi('qb:record:remove')")
    @Log(title = "考试记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(qbExamRecordService.deleteQbExamRecordByIds(ids));
    }
}
